package com.leo.blog.web.log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.AccessLimit;
import com.leo.blog.dto.LogExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.entity.Log;
import com.leo.blog.enums.LogStateEnum;
import com.leo.blog.service.LogService;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/log")
public class LogManagementController {
	@Autowired
	private LogService logService;
	
	/**
	 *  更新日志功能
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updatebyprimarykeyselective")
	@ResponseBody
	public Map<String, Object> updateByPrimaryKeySelective(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int logId = HttpServletRequestUtil.getInt(request, "logId");
		String logUserType = HttpServletRequestUtil.getString(request, "logUserType");
		String logIp = HttpServletRequestUtil.getString(request, "logIp");
		String logDescription = HttpServletRequestUtil.getString(request, "logDescription");
		String logParam = HttpServletRequestUtil.getString(request, "logParam");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			Log log = new Log();
			if(logId > 0) {
				log.setLogId(logId);
				if(logUserType != null && logUserType != "") {
					log.setLogUserType(logUserType);
				}
				if(logIp != null && logIp != "") {
					log.setLogIp(logIp);
				}
				if(logDescription != null && logDescription != "") {
					log.setLogDescription(logDescription);
				}
				if(logParam != null && logParam != "") {
					log.setLogParam(logParam);
				}
				LogExecution le = logService.updateByPrimaryKeySelective(log);
				if(le.getState() == LogStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败,Id为0");
			}	
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	/**
	 * 模糊组合分页查询访客信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getloglistbydate")
	@ResponseBody
	@AccessLimit(seconds=1,maxCount=10)
	public Map<String, Object> getLogListByDate(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		//获取前台传过来的值
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String logUserType = HttpServletRequestUtil.getString(request, "logUserType");
		String logIp = HttpServletRequestUtil.getString(request, "logIp");
		String logDescription = HttpServletRequestUtil.getString(request, "logDescription");
		
		Log logCondition = new Log();
		if(logDescription != null && logDescription != "") {
			logCondition.setLogDescription(logDescription);
		}
		if(logIp != null && logIp != "") {
			logCondition.setLogIp(logIp);
		}
		if(logUserType != null && logUserType != "") {
			logCondition.setLogUserType(logUserType);
		}
		if(startTime != null && startTime != "") {
			logCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			logCondition.setEndTime(endTime);
		}
		
		LogExecution le = logService.getLogListByDate(logCondition, pageIndex, pageSize);
		
		if(le.getState() == LogStateEnum.SUCCESS.getState()) {
			//返回页数
			int page = 0;
			int pageCount = le.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("logList", le.getLogList());
			modelMap.put("count", le.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据列表失败");
		}		
		return modelMap;
	}
	
	@RequestMapping(value = "/getuserlogbydate")
	@ResponseBody
	@AccessLimit(seconds=1,maxCount=10)
	public Map<String, Object> getUserLogByDate(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String logUserType = HttpServletRequestUtil.getString(request, "logUserType");
		
		Log logCondition = new Log();
		
		if(logUserType != null && logUserType != "") {
			logCondition.setLogUserType(logUserType);
		}
		if(startTime != null && startTime != "") {
			logCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			logCondition.setEndTime(endTime);
		}
		List<?> list = logService.getUserLogByDate(logCondition);
		if(list.size() > 0) {
			modelMap.put("list", list);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}
		return modelMap;
	}
}
